package com.openingedu.mba.common;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.dao.ForeignCollection;
import com.openingedu.mba.entity.PaperQuestionVO;
import com.openingedu.mba.entity.PaperVO;
import com.openingedu.mba.persist.DbPaper;
import com.openingedu.mba.persist.DbPaperQuestion;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class CommonUtil {

	private static final String TAG = CommonUtil.class.getSimpleName();

	/**
	 * @brief 判读输入的字符串参数是否为空
	 * @param str
	 *            输入的字符参数
	 * @return 返回字符串是否为空 true：字符串为空 false：字符串不为空
	 */
	public static boolean isNotNull(String str) {
		if (str != null && str.length() != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @brief 判读字符串是否为空
	 * @param str
	 *            要判断的字符串
	 * @return 为空返回true 不为空返回false
	 */
	public static boolean isNull(String str) {
		if (str == null || str.length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cm.getActiveNetworkInfo();
			if (info != null) {
				if (info.isConnected()) {
					Log.i(TAG, "Network is connected");
					return true;
				} else {
					Log.i(TAG, "Network is not connected");
					return false;
				}
			}
			return false;
		} catch (Exception e) {
			Log.e(TAG, "get NetworkInfo error." + e.getMessage(), e);
			return false;
		}
	}
	
	public static PaperVO db2VO(DbPaper dbPaper) {
		PaperVO paper = new PaperVO();
		paper.setPaperID(dbPaper.getPaperID());
		paper.setPaperDesc(dbPaper.getPaperDesc());
		paper.setPaperPrice(dbPaper.getPaperPrice());
		paper.setPaperSerial(dbPaper.getPaperSerial());
		paper.setPaperStatus(dbPaper.getPaperStatus());
		paper.setPaperTitle(dbPaper.getPaperTitle());
		paper.setPaperType(dbPaper.getPaperType());
		paper.setPaperTypeTitle(dbPaper.getPaperTypeTitle());
		paper.setPaperVersion(dbPaper.getPaperVersion());
		
		List<PaperQuestionVO> paperQuestionList = new ArrayList<PaperQuestionVO>();
		ForeignCollection<DbPaperQuestion> questions = dbPaper.getPaperQuestions();
		for (DbPaperQuestion dbPaperQuestion : questions) {
			PaperQuestionVO paperQuestionVO = new PaperQuestionVO();
			paperQuestionVO.setId(dbPaperQuestion.getId());
			paperQuestionVO.setQuestionID(dbPaperQuestion.getQuestionID());
			paperQuestionList.add(paperQuestionVO);
		}
		paper.setPaperQuestionList(paperQuestionList);
		return paper;
	}
	
	public static String percentStr(int y, int z) {  
		// 接受百分比的值 
        String percentStr = "";
        double pd = percent(y, z);
//        // 注释掉的也是一种方法
//		NumberFormat nf = NumberFormat.getPercentInstance();
//		nf.setMinimumFractionDigits(2);
        
        // NumberFormat nf = NumberFormat.getPercentInstance(); 注释掉的也是一种方法  
        // nf.setMinimumFractionDigits( 2 ); 保留到小数点后几位  
        // ##.00%
        // 百分比格式，后面不足2位的用0补齐  
        DecimalFormat df1 = new DecimalFormat("##.00%"); 
                                                            
        percentStr = df1.format(pd);  
        System.out.println(percentStr);  
        return percentStr;  
    }

	public static double percent(int y, int z) {
		double py = y * 1.0; 
        double pz = z * 1.0;
		return py / pz;
	}
}
